<?php
class Controller_Role extends FLEA_Controller_Action {
	function actionindex(){
		$table_role = FLEA::getSingleton('Model_roles');
		$data = $table_role->findAll();
		$smarty =& $this->_getView();
		$smarty->assign('role', $data);
		$smarty->display('role.html');
	}
	function actionaddrole(){
		if($this->_isPOST()){
            $table_role = FLEA::getSingleton('Model_roles');
            $row = array(
                'rolename' => strip_tags($_POST['name']),
            );
            $table_role->save($row);
		}
       redirect($this->_url());
	}
	function actioneditrole(){
        $table_role = & FLEA::getSingleton('Model_roles');
        $table_user = & FLEA::getSingleton('Model_users');
		if($this->_isPOST()){
            $roles = $_POST['roles'];
            $row['user_id'] = $_POST['user_id'];
            foreach($roles as $key => $value){
                $row[$table_user->rolesField][] = $value;
            }
            if($table_user->save($row)){
                js_alert('更新成功','history.back();');
            }
            else{
                js_alert('更新失败，请重试或联系管理员','history.back();');
            }
		}
        else{
            $user_id = (int)$_GET['user_id'];
            $user = $table_user->find($user_id);
            $roles = $table_role->findAll();
            $smarty =& $this->_getView();
            $smarty->assign('roles', $roles);
            $smarty->assign('user', $user);
            $smarty->display('editrole.html');
        }
	}

	function actiondelrole(){
		$role_id = (int)$_GET['role_id'];
		$table_role = FLEA::getSingleton('Model_roles');
		$table_role->removeByPkv($role_id);
		redirect($this->_url());
	}
}
